<template>
  <l-layout class="l-tab-page" v-if="!lr_loadPage">
    <template #left>
      <l-panel :title="$t('文件分类')" style="padding-right: 0">
        <div class="l-rblock" style="overflow: hidden auto">
          <l-side-list :list="sideList" @click="handleSideClick" />
        </div>
      </l-panel>
    </template>
    <l-panel style="padding-left: 0">
      <template #title>
        <l-breadcrumb :levelList.sync="levelList" @change="handleBreadcrumbChange"></l-breadcrumb>
      </template>
      <template #toolLeft>
        <div class="l-panel--item">
          <el-input :placeholder="$t('搜索文件')" @keyup.enter.native="hanleSearch" v-model="searchWord" size="mini">
            <el-button @click="hanleSearch" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </div>
      </template>
      <template #toolRight>
        <l-tool-btns :hasAdd="false">
          <template v-if="levelList[0].id == 'learun_folder_all'">
            <el-button-group>
              <el-button v-if="isAddFolder" size="mini" @click="handleAddFolder()">{{ $t("新建文件夹") }}</el-button>
              <el-button v-if="isAuthFolder" size="mini" @click="handleAddAuth()">{{ $t("授权") }}</el-button>
            </el-button-group>
            <el-button-group v-if="isUpLoadFile">
              <el-button size="mini" type="primary" @click="handleAddFile()">{{
                $t("上传文件")
              }}</el-button>
            </el-button-group>
          </template>
        </l-tool-btns>
      </template>
      <div v-if="levelList[0].id == 'learun_folder_audit'" class="l-rblock" style="padding: 8px 8px 0 8px">
        <div class="l-auto-window">
          <el-tabs v-model="activeName" type="card" @tab-click="wfTabClick">
            <el-tab-pane class="l-tabs-container" :label="$t('待审文件')" name="tab0">
              <l-wf-audit-table code="wffile" :loadFormDataList="loadFileListByWf" :columns="wfcolumns1" ref="tab0">
                <template v-slot:f_Name="scope">
                  <div class="learun-list-item-wraper">
                    <div class="learun-list-item-icon">
                      <i :class="lr_getFileIcon(scope.row.f_Name).icon" :style="{
                          color: lr_getFileIcon(scope.row.f_Name).color,
                          'font-size': '32px',
                        }"></i>
                    </div>
                    <div class="learun-list-item-content">
                      <div class="learun-list-item-title">
                        <el-button @click="handleOpenWFAudit(scope.row)" size="mini"
                          type="text">{{ scope.row.f_Name }}</el-button>
                      </div>
                    </div>
                  </div>
                </template>
              </l-wf-audit-table>
            </el-tab-pane>
            <el-tab-pane class="l-tabs-container" :label="$t('我的文件')" name="tab1">
              <l-wf-audit-table code="wffile" type="my" :loadFormDataList="loadFileListByWf" :columns="wfcolumns2"
                ref="tab1">
                <template v-slot:f_Name="scope">
                  <div class="learun-list-item-wraper">
                    <div class="learun-list-item-icon">
                      <i :class="lr_getFileIcon(scope.row.f_Name).icon" :style="{
                          color: lr_getFileIcon(scope.row.f_Name).color,
                          'font-size': '32px',
                        }"></i>
                    </div>
                    <div class="learun-list-item-content">
                      <div class="learun-list-item-title">
                        <el-button @click="handleOpenWFAudit(scope.row)" size="mini"
                          type="text">{{ scope.row.f_Name || "异常文件" }}</el-button>
                      </div>
                    </div>
                  </div>
                </template>
              </l-wf-audit-table>
            </el-tab-pane>
            <el-tab-pane class="l-tabs-container" :label="$t('已审文件')" name="tab2">
              <l-wf-audit-table code="wffile" type="completed" :loadFormDataList="loadFileListByWf"
                :columns="wfcolumns3" ref="tab2">
                <template v-slot:f_Name="scope">
                  <div class="learun-list-item-wraper">
                    <div class="learun-list-item-icon">
                      <i :class="lr_getFileIcon(scope.row.f_Name).icon" :style="{
                          color: lr_getFileIcon(scope.row.f_Name).color,
                          'font-size': '32px',
                        }"></i>
                    </div>
                    <div class="learun-list-item-content">
                      <div class="learun-list-item-title">
                        <el-button @click="handleOpenWFAudit(scope.row)" size="mini" type="text">{{
                            `${scope.row.f_Name || this.$t("异常文件")}(${
                              scope.row.wf_f_UnitName
                            })`
                          }}</el-button>
                      </div>
                    </div>
                  </div>
                </template>
              </l-wf-audit-table>
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
      <div v-else class="l-rblock">
        <l-table :columns="columns" :dataSource="tableData" :loading="tableLoading">
          <template v-slot:name="scope">
            <div class="learun-list-item-wraper">
              <div class="learun-list-item-icon">
                <i v-if="scope.row.type == 1" class="el-icon-folder" style="color: #fbbc06; font-size: 32px"></i>
                <i v-else :class="lr_getFileIcon(scope.row.name).icon" :style="{
                    color: lr_getFileIcon(scope.row.name).color,
                    'font-size': '32px',
                  }"></i>
              </div>
              <div class="learun-list-item-content">
                <div class="learun-list-item-title">
                  <el-button @click="handleOpenFolder(scope.row)" size="mini"
                    type="text">{{ scope.row.name }}</el-button>
                </div>
                <div v-if="
                    isSearchWord || levelList[0].id == 'learun_folder_delete'
                  " class="learun-list-item-des">
                  {{ scope.row.level }}
                </div>
              </div>
            </div>
          </template>

          <template v-slot:size="scope">
            {{ scope.row.type == 1 ? "" : $countFileSize(scope.row.size) }}
          </template>
          <l-table-btns :filterBtns="filterBtns" :btns="tableBtns" :isAuth="false"
            @click="lr_handleTableBtnClick"></l-table-btns>
        </l-table>
      </div>
    </l-panel>

    <!--新增文件夹-->
    <l-dialog :title="$t(folderTitle)" :visible.sync="folderVisible" :height="120" :width="400" @ok="handleFolderSave"
      @close="handleFolderCloseForm" @opened="handleFolderOpenedForm">
      <folder-form ref="folderForm"></folder-form>
    </l-dialog>

    <!--选择文件夹-->
    <l-dialog :title="`${$t(folderSelectTitle)}到【${folderSelect.name}】`" :visible.sync="folderSelectVisible"
      :height="440" :width="600" @ok="handleFolderSelectSave" @close="handleFolderSelectClose"
      @opened="handleFolderSelectOpened">
      <folder-select-form ref="folderSelectForm" @select="handleSelectFolder"></folder-select-form>
    </l-dialog>

    <!--文件预览-->
    <l-fullscreen-dialog :showOk="false" :visible.sync="fileVisible">
      <template #title>
        <div>
          <div v-if="fileData" class="learun-list-item-wraper">
            <div class="learun-list-item-icon">
              <i :class="lr_getFileIcon(fileData.name).icon"
                :style="{ color: lr_getFileIcon(fileData.name).color }"></i>
            </div>
            <div class="learun-list-item-content">
              <div class="learun-list-item-title" style="color: rgba(0, 0, 0, 0.85)">
                {{ fileData.name }}
              </div>
              <div class="learun-list-item-des">
                {{ fileData.level || levelName }}
              </div>
            </div>
          </div>
        </div>
      </template>
      <file-form ref="fileForm" :file="fileData" :level="levelName">
      </file-form>
    </l-fullscreen-dialog>

    <!--文件命名-->
    <l-dialog :title="$t('重命名')" :visible.sync="fileNameVisible" :height="120" :width="400" @ok="handleFileNameSave"
      @close="handleFileNameCloseForm" @opened="handleFileNameOpenedForm">
      <file-name-form ref="fileNameForm"></file-name-form>
    </l-dialog>

    <!--流程创建-->
    <wf-create-form ref="wffile" code="wffile" :title="$t('上传文件')" :visible.sync="wfVisible" :params="{
        folder: wfFolderId,
      }" />

    <!--上传文件-->
    <!-- <l-dialog :title="`${$t('上传文件')}`" :visible.sync="wfVisible" ref="wffile" :height="480" :width="800"
      @ok="handleSaveUpload" @opened="handleOpenedUpload" @close="wfVisible = false">
      <fileWfForm ref="fileWfForm" :title="$t('上传文件')" code="wffile" :visible.sync="wfVisible" :params="{
        folder: wfFolderId,
      }"></fileWfForm>
    </l-dialog> -->

    <!--文件授权-->
    <l-dialog :title="`${$t('文件授权')}-${authTitile}`" :visible.sync="authVisible" :hasBtns="false" :height="480"
      :width="800" @opened="handleAuthOpened">
      <auth-index ref="authIndex" :fileInfoId="authFileId" :isFolder="isFolder"></auth-index>
    </l-dialog>
  </l-layout>
</template>
<script>
const api = window.$api.system.file;

import FolderForm from "./folderForm.vue";
import FolderSelectForm from "./folderSelectForm.vue";
import FileForm from "./fileForm.vue";
import FileNameForm from "./fileNameForm.vue";
import fileWfForm from "./fileWfForm.vue";
import AuthIndex from "./auth/index.vue";

export default {
  components: {
    FolderForm,
    FolderSelectForm,
    FileForm,
    FileNameForm,
    AuthIndex,
    fileWfForm,
  },
  data() {
    return {
      sideList: [
        {
          icon: "el-icon-folder",
          label: this.$t("正式文件"),
          value: "all",
        },
        {
          icon: "learun-icon-signature",
          label: this.$t("审核文件"),
          value: "audit",
        },
        {
          icon: "el-icon-delete",
          label: this.$t("回收站"),
          value: "delete",
        },
      ],
      levelList: [
        {
          name: this.$t("正式文件"),
          id: "learun_folder_all",
        },
      ],
      columns: [
        {
          label: this.$t("文件名"),
          prop: "name",
          minWidth: 200,
          isNotAutoWrap: false
        },
        {
          label: this.$t("创建者"),
          prop: "createUserName",
          width: 100,
        },
        {
          label: this.$t("最后更新"),
          prop: "modifyDate",
          width: 160,
          dataType: "datetime",
        },
        {
          label: this.$t("文件大小"),
          prop: "size",
          width: 100,
        },
      ],
      tableLoading: false,
      tableData: [],

      isSearchWord: false,
      searchWord: "",

      formEditRow: null,

      folderTitle: "",
      folderVisible: false,
      folderEdit: false,

      folderSelectTitle: "",
      folderSelectVisible: false,
      folderSelectType: "", // 选择类型，移动或者复制
      folderSelect: {
        name: this.$t("根目录"),
        id: "learun_folder_all",
      },

      fileOption: [], // 文件上传
      fileUploadTimer: null,
      fileSelectVisible: false,
      fileSameList: [],

      fileVisible: false, // 文件预览
      fileData: null,

      fileNameVisible: false, // 文件命名

      // 文件审核流程
      wfVisible: false,
      activeName: "tab0",
      wfFolderId: "",

      wfcolumns1: [
        // 待办
        {
          label: this.$t("文件名"),
          prop: "f_Name",
          minWidth: 200,
          isNotAutoWrap: false
        },
        { label: this.$t("状态"), prop: "wf_f_IsUrge", width: 60, align: "center" },
        {
          label: this.$t("提交人"),
          prop: "wf_f_ProcessUserName",
          width: 100,
          align: "left",
        },
        { label: this.$t("时间"), prop: "wf_f_CreateDate", width: 160, align: "left" },
      ],
      wfcolumns2: [
        // 我的
        {
          label: this.$t("文件名"),
          prop: "f_Name",
          minWidth: 200,
          isNotAutoWrap: false
        },
        { label: this.$t("状态"), prop: "wf_f_IsFinished", width: 60, align: "center" },
        {
          label: this.$t("提交人"),
          prop: "wf_f_CreateUserName",
          width: 100,
          align: "left",
        },
        { label: this.$t("时间"), prop: "wf_f_CreateDate", width: 160, align: "left" },
      ],
      wfcolumns3: [
        {
          label: this.$t("文件名"),
          prop: "f_Name",
          minWidth: 200,
          isNotAutoWrap: false
        },
        { label: this.$t("状态"), prop: "wf_f_IsFinished", width: 60, align: "center" },
        {
          label: this.$t("提交人"),
          prop: "wf_f_CreateUserName",
          width: 100,
          align: "left",
        },
        { label: this.$t("时间"), prop: "wf_f_CreateDate", width: 160, align: "left" },
      ],

      // 权限
      authVisible: false,
      authTitile: "",
      authFileId: "",
      isFolder: 1,
      rootAuthType: "",
    };
  },
  computed: {
    tableBtns() {
      console.log('tableBtns')
      if (this.levelList[0].id == "learun_folder_delete") {
        return [
          { prop: "Recovery", label: this.$t("还原") },
          { prop: "Delete", label: this.$t("删除"), divided: true },
        ];
      } else {
        return [
          { prop: "Upload", label: this.$t("上传") },
          { prop: "Auth", label: this.$t("授权") },
          { prop: "Down", label: this.$t("下载") },
          { prop: "Edit", label: this.$t("重命名") },
          { prop: "Move", label: this.$t("移动到...") },
          { prop: "VDelete", label: this.$t("删除"), divided: true },
        ];
      }
    },
    levelName() {
      const list = this.levelList.map((t) => t.name);
      const listStr = String(list);
      return listStr.replace(/,/g, "/");
    },
    currentLevel() {
      return this.levelList[this.levelList.length - 1];
    },
    currentAuthType() {
      let authType = this.rootAuthType;
      if (this.currentLevel.id != "learun_folder_all") {
        authType = this.currentLevel.authType;
      }
      return authType;
    },

    isAddFolder() {
      if (this.lr_isSystem) {
        return true;
      }
      return this.currentAuthType.indexOf("10") > -1;
    },
    isAuthFolder() {
      if (this.lr_isSystem) {
        return true;
      }
      return this.currentAuthType.indexOf("13") > -1;
    },
    isUpLoadFile() {
      if (this.lr_isSystem) {
        return true;
      }
      return this.currentAuthType.indexOf("10") > -1;
    },
  },
  async mounted() {
    // 加载根目录权限
    const rootAuth = (await this.$awaitWraper(api.getRootAuth())) || {};
    this.rootAuthType = rootAuth.f_AuthType || "";
    this.loadTableData();
  },
  methods: {
    handleSideClick({ label, value }) {
      this.levelList = [
        {
          name: label,
          id: `learun_folder_${value}`,
        },
      ];
      this.searchWord = "";

      if (value == "audit") {
        this.$nextTick(() => {
          this.$refs[this.activeName] && this.$refs[this.activeName].loadData();
        });
      } else {
        this.loadTableData();
      }
    },
    // 按钮权限过滤
    filterBtns(row, btns) {
      if (row.type == 1) {
        // 文件夹
        const downBtnIndex = btns.findIndex((t) => t.prop == "Down");
        if (downBtnIndex > -1) {
          btns.splice(downBtnIndex, 1);
        }

        if (this.lr_isSystem) {
          return btns;
        }

        if (!row.authType) {
          return [];
        }

        const uploadBtn = btns.find((t) => t.prop == "Upload");
        if (uploadBtn && row.authType.indexOf("14") == -1) {
          uploadBtn.disabled = true;
        }

        const authBtn = btns.find((t) => t.prop == "Auth");
        if (authBtn && row.authType.indexOf("13") == -1) {
          authBtn.disabled = true;
        }

        const editBtn = btns.find((t) => t.prop == "Edit");
        if (editBtn && row.authType.indexOf("15") == -1) {
          editBtn.disabled = true;
        }

        const moveBtn = btns.find((t) => t.prop == "Move");
        if (moveBtn && row.authType.indexOf("12") == -1) {
          moveBtn.disabled = true;
        }

        const vdeleteBtn = btns.find((t) => t.prop == "VDelete");
        if (vdeleteBtn && row.authType.indexOf("11") == -1) {
          vdeleteBtn.disabled = true;
        }
      } else {
        if (this.lr_isSystem) {
          return btns;
        }
        if (!row.authType) {
          return [];
        }

        const uploadBtn = btns.find((t) => t.prop == "Upload");
        if (uploadBtn && row.authType.indexOf("02") == -1) {
          uploadBtn.disabled = true;
        }

        const downBtn = btns.find((t) => t.prop == "Down");
        if (downBtn && row.authType.indexOf("03") == -1) {
          downBtn.disabled = true;
        }

        const authBtn = btns.find((t) => t.prop == "Auth");
        if (authBtn && row.authType.indexOf("07") == -1) {
          authBtn.disabled = true;
        }

        const editBtn = btns.find((t) => t.prop == "Edit");
        if (editBtn && row.authType.indexOf("08") == -1) {
          editBtn.disabled = true;
        }

        const moveBtn = btns.find((t) => t.prop == "Move");
        if (moveBtn && row.authType.indexOf("04") == -1) {
          moveBtn.disabled = true;
        }

        const vdeleteBtn = btns.find((t) => t.prop == "VDelete");
        if (vdeleteBtn && row.authType.indexOf("05") == -1) {
          vdeleteBtn.disabled = true;
        }

        // 回收站
        const recoveryBtn = btns.find((t) => t.prop == "Recovery");
        if (recoveryBtn && row.authType.indexOf("06") == -1) {
          recoveryBtn.disabled = true;
        }

        const deleteBtn = btns.find((t) => t.prop == "Delete");
        if (deleteBtn && row.authType.indexOf("09") == -1) {
          deleteBtn.disabled = true;
        }
      }
      return btns;
    },

    // 列表数据获取
    hanleSearch() {
      if (this.levelList[0].id == 'learun_folder_audit') {
        this.$refs[this.activeName] && this.$refs[this.activeName].loadData({ formKeyword: this.searchWord });
      }
      else {
        this.loadTableData();
      }
    },
    async loadTableData() {
      this.tableLoading = true;
      const queryData = {
        keyword: this.searchWord,
        pid: this.levelList[this.levelList.length - 1].id,
        order: "f_Name",
      };
      let data = [];
      if (queryData.pid == "learun_folder_delete") {
        data = await this.$awaitWraper(api.getRecycles(queryData));
      } else {
        data = await this.$awaitWraper(api.getList(queryData));
      }

      this.tableData = data || [];
      this.isSearchWord = this.searchWord ? true : false;

      this.tableLoading = false;
    },

    // 文件操作
    handleAddFile() {
      this.wfFolderId = this.currentLevel.id;
      this.wfVisible = true;
    },
    handleUpload($index, row) {
      if (row.type == 1) {
        this.wfFolderId = row.id;
        this.wfVisible = true;
      } else {
        this.wfVisible = true;
      }
    },

    //文件命名
    async handleFileNameSave(showLoading, hideLoading) {
      showLoading("保存数据中...");
      if (await this.$refs.fileNameForm.validateForm()) {
        const res = await this.$refs.fileNameForm.saveForm({
          keyValue: this.formEditRow.id,
        });
        if (res) {
          const msg = this.$t("重命名成功!");
          this.$message({
            type: "success",
            message: this.$t(msg),
          });
          this.loadTableData();
          this.fileNameVisible = false;
        }
      }
      hideLoading();
    },
    handleFileNameCloseForm() {
      this.$refs.fileNameForm.resetForm();
    },
    handleFileNameOpenedForm() {
      this.$refs.fileNameForm.setForm({
        f_Name: this.formEditRow.name,
        f_Id: this.formEditRow.id,
        f_FolderId: this.formEditRow.pid,
      });
    },

    handleDown($index, row) {
      this.$downFile(
        `${this.apiUrl}system/annexesfile/${row.fileId}?token=${this.token}`
      );
    },

    /**
     * 文件夹管理
     */
    handleAddFolder() {
      this.folderEdit = false;
      this.showFolderForm("新增文件夹");
    },
    handleEdit($index, row) {
      // 重命名
      this.formEditRow = row;
      if (row.type == 1) {
        // 文件夹
        this.folderEdit = true;
        this.showFolderForm(`${this.$t('重命名')}【${row.name}】`);
      } else {
        // 文件
        this.fileNameVisible = true;
      }
    },
    showFolderForm(text) {
      this.folderTitle = text;
      this.folderVisible = true;
    },
    async handleFolderSave(showLoading, hideLoading) {
      showLoading("保存数据中...");
      if (
        await this.$refs.folderForm.validateForm(
          this.folderEdit
            ? this.formEditRow.pid
            : this.levelList[this.levelList.length - 1].id
        )
      ) {
        const res = await this.$refs.folderForm.saveForm({
          keyValue: this.folderEdit ? this.formEditRow.id : "",
          pid: this.levelList[this.levelList.length - 1].id,
          levelId: String(this.levelList.map((t) => t.id)).replace(/,/g, "/"),
          isEdit: this.folderEdit,
        });
        if (res) {
          const msg = this.formEdit ? this.$t("重命名成功!") : this.$t("新增成功!");
          this.$message({
            type: "success",
            message: this.$t(msg),
          });
          this.loadTableData();
          this.folderVisible = false;
        }
      }
      hideLoading();
    },
    handleFolderOpenedForm() {
      if (this.folderEdit) {
        this.$refs.folderForm.setForm({
          f_Name: this.formEditRow.name,
          f_Id: this.formEditRow.id,
          f_PId: this.formEditRow.pid,
        });
      }
    },
    handleFolderCloseForm() {
      this.$refs.folderForm.resetForm();
    },

    handleMove($index, row) {
      this.formEditRow = row;
      this.showFolderSelectForm(`${this.$t('移动')}【${row.name}】`);
    },

    handleOpenFolder(row) {
      if (row.type == 1) {
        // 文件夹
        let levelList = this.levelList;
        if (row.level) {
          levelList = [];
          const levelNameList = row.level.split("/");
          const levelIdList = row.levelId.split("/");
          levelNameList.forEach((name, index) => {
            levelList.push({ name: name, id: levelIdList[index] });
          });
        }
        levelList.push({
          name: row.name,
          id: row.id,
          authType: row.authType,
        });
        this.levelList = levelList;
        this.searchWord = "";

        this.loadTableData();
      } else {
        // 预览文件
        if (this.lr_isSystem || row.authType.indexOf("1") != -1) {
          this.fileData = row;
          this.fileVisible = true;
        }
      }
    },
    handleBreadcrumbChange() {
      this.searchWord = "";
      this.loadTableData();
    },

    // 移动
    async handleFolderSelectSave(showLoading, hideLoading) {
      showLoading("提交中...");
      let msg = "";
      let res = null;
      msg = this.$t("移动成功");
      // 移动
      if (this.formEditRow.type == 1) {
        // 文件夹
        res = await this.$awaitWraper(
          api.moveFolder(this.formEditRow.id, this.folderSelect.id)
        );
      }
      else {
        // 文件
        res = await this.$awaitWraper(
          api.moveFile(this.formEditRow.id, this.folderSelect.id)
        );
      }

      if (res) {
        this.$message({
          type: "success",
          message: this.$t(msg),
        });
      }

      this.folderSelectVisible = false;
      hideLoading();
      this.loadTableData();
    },
    handleFolderSelectClose() {
      this.folderSelect = {
        name: this.$t("根目录"),
        id: "learun_folder_all",
      };
    },
    async handleFolderSelectOpened(showLoading, hideLoading) {
      showLoading("加载文件列表");
      await this.$refs.folderSelectForm.init(this.formEditRow);
      hideLoading();
    },
    showFolderSelectForm(text) {
      this.folderSelectTitle = text;
      this.folderSelectVisible = true;
    },
    handleSelectFolder(row) {
      if (row.id == "learun_folder_all") {
        this.folderSelect = {
          name: this.$t("根目录"),
          id: "learun_folder_all",
        };
      } else {
        this.folderSelect = row;
      }
    },
    // 删除
    handleVDelete($index, row) {
      // 虚拟删除
      this.$confirm(
        `${this.$t('是否删除')}${row.type == 1 ? this.$t("文件夹") : this.$t("文件")}【${row.name}】?`,
        "提示",
        {
          confirmButtonText: this.$t("确定"),
          cancelButtonText: this.$t("取消"),
          type: "warning",
        }
      )
        .then(async () => {
          let res;
          if (row.type == 1) {
            res = await this.$awaitWraper(api.deleteFolder(row.id));
          } else {
            res = await this.$awaitWraper(
              api.vdeleteFile(row.id, row.level || this.levelName)
            );
          }
          if (res) {
            this.loadTableData();
            this.$message({
              type: "success",
              message: this.$t("删除成功!"),
            });
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },

    // 彻底删除
    handleDelete($index, row) {
      // 永久删除
      this.$confirm(`${this.$t('是否永久删除文件')}【${row.name}】?`, this.$t("提示"), {
        confirmButtonText: this.$t("确定"),
        cancelButtonText: this.$t("取消"),
        type: "warning",
      })
        .then(async () => {
          const res = await this.$awaitWraper(api.deleteFile(row.id));
          if (res) {
            this.loadTableData();
            this.$message({
              type: "success",
              message: this.$t("删除成功!"),
            });
          }
        })
        .catch(() => { });
    },

    handleRecovery($index, row) {
      // 还原
      this.$confirm(
        `${this.$t('是否还原文件')}【${row.name}】?`,
        this.$t("提示"),
        {
          confirmButtonText: this.$t("确定"),
          cancelButtonText: this.$t("取消"),
          type: "warning",
        }
      )
        .then(async () => {
          await this.$awaitWraper(api.restoreFile(row.id));
          this.loadTableData();
          this.$message({
            type: "success",
            message: this.$t("还原成功!"),
          });
        })
        .catch(() => { });
    },


    // 审批文件
    wfTabClick() {
      this.$nextTick(() => {
        this.$refs[this.activeName] && this.$refs[this.activeName].loadData();
      });
    },
    handleOpenWFAudit(row) {
      if (!row.f_Name) {
        this.$message({
          type: "warning",
          message: this.$t("文件异常,无法找到上传文件!"),
        });
        return;
      }
      this.$refs[this.activeName].handleOpen(row);
    },
    async loadFileListByWf(ids) {
      const list = await this.$awaitWraper(api.getListByWF(ids));
      const res = {};
      if (list) {
        list.forEach((t) => {
          t.f_ProcessTitle = t.f_Name;
          res[t.f_Id] = t;
        });
      }
      return res;
    },

    // 授权
    handleAddAuth() {
      this.authTitile = this.levelName;
      this.authFileId = this.currentLevel.id;
      this.isFolder = 1;
      this.authVisible = true;
    },
    handleAuth($index, row) {
      this.authTitile = this.levelName;
      this.authFileId = row.id;
      this.isFolder = row.type;
      this.authVisible = true;
    },
    handleAuthOpened() {
      this.$refs.authIndex.init();
    },

    async handleOpenedUpload(showLoading, hideLoading) {
      showLoading("加载数据中...");
      await this.$refs.fileWfForm.init({
        folder: this.wfFolderId,
      })
      hideLoading();
    },

    async handleSaveUpload(showLoading, hideLoading) {
      showLoading("保存数据中...");
      if (await this.$refs.fileWfForm.validateForm()) {
        const res = await this.$refs.fileWfForm.saveForm({ isEdit: false })
        if (res) {
          this.$message({
            type: 'success',
            message: '提交成功'
          })
        }
      }
      hideLoading();
    },
  },
};
</script>